package com.eastdigit.system.controller;

import com.eastdigit.servlet.DeptPermValidate;
import com.eastdigit.servlet.ReqBean;
import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;
import com.eastdigit.system.model.RoleMenu;
import com.eastdigit.system.service.IRoleService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/roleMenu")
public class RoleMenuController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping(value = "/{roleId}")
    @ResponseBody
    public JSONObject queryRoleMenu(@PathVariable String roleId) {
        JSONArray array = new JSONArray();
        for (RoleMenu roleMenu : roleService.queryRoleMenu(roleId)) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("menuId", roleMenu.getMenuId());
            array.add(jsonObject);
        }
        return Response.response(array);
    }

    @RequestMapping(value = "/{roleId}", method = RequestMethod.PUT)
    @ResponseBody
    @DeptPermValidate(id = "roleId", deptId = "deptId")
    public JSONObject addRoleMenu(@PathVariable String roleId, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        roleService.addRoleMenu(roleId, reqBean.getInfo("menuIds"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/{roleId}", method = RequestMethod.DELETE)
    @ResponseBody
    @DeptPermValidate(id = "roleId", deptId = "deptId")
    public JSONObject removeRoleMenu(@PathVariable String roleId, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        roleService.deleteRoleMenu(roleId, reqBean.getInfo("menuIds"));
        return Response.responseSuccess();
    }

}
